package com.zhou.juc.test08_broadenview;

import java.util.concurrent.Exchanger;

/**
 * @author zhouyanxiang
 * @create 2020-08-2020/8/15-10:33
 */
public class T12_TestExchanger  {

    static Exchanger<String> exchanger = new Exchanger<>();

    public static void main(String[] args) {
        new Thread(()->{
            String s = "T1";
            try {
                s = exchanger.exchange(s);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " " + s);
        },"t1").start();

        new Thread(()->{
            String s = "T2";
            try {
                s = exchanger.exchange(s);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " " + s);
        },"t2").start();
        // 只能允许两台交换，不能构建三个线程交换，如果三个之后那就是会阻塞了
//        new Thread(()->{
//            String s = "T3";
//            try {
//                s = exchanger.exchange(s);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println(Thread.currentThread().getName() + " " + s);
//        },"t3").start();
    }



}
